#include "buttons.h"
#include "shapes.h"
#include "myinputs.h"
#include "mydrawengine.h"
#include "vector2D.h"

MyInputs* pTheInputs = MyInputs::GetInstance();

Button::Button()
{
	isActive = false;
}

Button::~Button()
{

}

bool Button::hovering(MousePointer mouse)
{

	if (Button1.Intersects(mouse.GetPoint()) == true)
	{
	    return true;
	}
	else
	{
		return false;
	}

}

void Button::onClick()
{
	MyInputs* pMyInputs = MyInputs::GetInstance();
	if (isHovering && pMyInputs->IfMouseNewLeftDown())
	{
		isActive = true;
	}
	if (!isHovering || !pMyInputs->IfMouseNewLeftDown())
	{
		isActive = false;
	}
}


void Button::Initialise(float top, float left, float bottom, float right, char text[15])
{
	Button1.PlaceAt(top, left, bottom, right);
	HBCorner1 = Button1.GetCorner1();
	HBCorner2 = Button1.GetCorner2();
	HBCorner1.XValue = HBCorner1.XValue -5;
	HBCorner1.YValue = HBCorner1.YValue -5;
	HBCorner2.XValue = HBCorner2.XValue +5;
	HBCorner2.YValue = HBCorner2.YValue +5;


	HoverBox.PlaceAt(HBCorner1, HBCorner2);

	strcpy_s(theText,text);
}

void Button::Update(MousePointer mouse)
{
	isHovering = hovering(mouse);
	onClick();
}

void Button::Draw()
{
	if(isActive)
	{
		MyDrawEngine::GetInstance()->FillRect(HoverBox, MyDrawEngine::BLUE);
	}
	else if(isHovering)
	{
		MyDrawEngine::GetInstance()->FillRect(HoverBox, MyDrawEngine::YELLOW);
	}
	else
	{
		MyDrawEngine::GetInstance()->FillRect(HoverBox, MyDrawEngine::GREY);
	}
	MyDrawEngine::GetInstance()->FillRect(Button1, MyDrawEngine::WHITE);

	int x = (int)Button1.GetTopLeft().XValue;
	int y = (int)Button1.GetTopLeft().YValue;

	MyDrawEngine::GetInstance()->WriteText(x, y, theText, MyDrawEngine::BLUE, 0);
}

bool Button::getActive()
{
	return isActive;
}